package com.qzdsoft.erpcloud.service.proj.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.proj.MaterialInfo;
import com.qzdsoft.erpcloud.domain.proj.MaterialInfoKey;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.mapper.proj.MaterialInfoMapper;
import com.qzdsoft.erpcloud.mapper.proj.MaterialInfoViMapper;
import com.qzdsoft.erpcloud.service.proj.MaterialInfoService;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfoQueryInfo;
import com.qzdsoft.erpcloud.vo.proj.ProjMaterialBomInfo;
import com.qzdsoft.erpcloud.vo.proj.ProjMaterialInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;

/**
 * Created by Admin
 */
@Service
public class  MaterialInfoServiceImpl implements MaterialInfoService {
	@Autowired
    private MaterialInfoMapper materialInfoMapper;
	//引入视图操作
	@Autowired	
	private MaterialInfoViMapper materialInfoViMapper;
	
	/**
	 * 查询带回网格数据  带分页信息
	 */
	@Override
	public LayTableResponse<ProjMaterialInfo> findPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<ProjMaterialInfo> materialInfo = materialInfoViMapper.findAll();
        LayTableResponse<ProjMaterialInfo> po = new LayTableResponse<ProjMaterialInfo>(materialInfo);
        return po;
	}
	/**
	 * 新增存货信息
	 */
	@Override
	public Response<String> addMaterial(MaterialInfo materialInfo, LoginUser loginUser) throws Exception {
		//判断Form 提交是否为空
    	if(materialInfo==null){
    		throw new Exception("提交数据为空，不可保存！！");
    	}
    	materialInfo.setCompanyId(loginUser.getCompanyId());
    	materialInfo.setCtime(new Date());
    	materialInfo.setCreatorId(1);
    	materialInfoMapper.insert(materialInfo);
	        return Response.success("新增存货信息成功");
	}
	/**
	 * 根据ID 查询唯一一条物料信息
	 */
	@Override
	public MaterialInfo getById(Integer id) {
		MaterialInfo materialInfo = new MaterialInfo();
		materialInfo.setId(id);
		return this.materialInfoMapper.selectByPrimaryKey(materialInfo);
	}
	/**
	 * 编辑保存事件
	 */
	@Override
	public Response<String> editMaterial(MaterialInfo materialInfo, LoginUser loginUser) {
		MaterialInfo searchMaterialInfo = this.getById(materialInfo.getId());
		if(searchMaterialInfo==null){
			return  Response.error("物料信息修改失败，当前物料信息未找到！！");
		}
		searchMaterialInfo.setBrandId(materialInfo.getBrandId());
		searchMaterialInfo.setMeasurementUId(materialInfo.getMeasurementUId());
		searchMaterialInfo.setMaterialCoding(materialInfo.getMaterialCoding());
		searchMaterialInfo.setMaterialName(materialInfo.getMaterialName());
		searchMaterialInfo.setMaterialSpec(materialInfo.getMaterialSpec());
		searchMaterialInfo.setRemark(materialInfo.getRemark());
		searchMaterialInfo.setMinimumPackingQuantity(materialInfo.getMinimumPackingQuantity());
		searchMaterialInfo.setMaterialPriceTypeId(materialInfo.getMaterialPriceTypeId());
		searchMaterialInfo.setMaterialTypeId(materialInfo.getMaterialTypeId());
		searchMaterialInfo.setIsBuy(materialInfo.getIsBuy());
		searchMaterialInfo.setIsNoUsed(materialInfo.getIsNoUsed());
		searchMaterialInfo.setIsOutsource(materialInfo.getIsOutsource());
		searchMaterialInfo.setIsProdConsumption(materialInfo.getIsProdConsumption());
		searchMaterialInfo.setIsSale(materialInfo.getIsBuy());
		searchMaterialInfo.setIsVirtual(materialInfo.getIsVirtual());
		int id = materialInfoMapper.updateByPrimaryKey(searchMaterialInfo);
		MaterialInfo returnEn = this.getById(id);
		if(returnEn==null){
		    return Response.error("物料信息修改失败！！！");
		}else
		return Response.success("物料信息修改成功！！！");
	}
	/**
	 * 删除物料信息
	 */
	@Override
	public Response<String> delteMaterial(Integer id, LoginUser loginUser) {
		MaterialInfo searchMaterialInfo = this.getById(id);
		if(searchMaterialInfo==null){
			return  Response.error("物料信息删除失败，当前物料信息未找到！！");
		}
		MaterialInfoKey materialkey =new MaterialInfoKey();
		materialkey.setId(id);
		this.materialInfoMapper.deleteByPrimaryKey(materialkey);
		return Response.success("物料信息【"+searchMaterialInfo.getMaterialCoding()+"】删除成功");
	}
	
	/**
	 * 根据ID 查询唯一一条视图数据
	 */
	@Override
	public ProjMaterialInfo getViewById(Integer id) {
		return this.materialInfoViMapper.selectByPrimaryKey(id);
	}
	/**
	 * 根据表头的查询条件过滤网格数据
	 * @see com.qzdsoft.erpcloud.service.proj.MaterialInfoService#findPageByItems(com.qzdsoft.erpcloud.vo.proj.MaterialInfoQueryInfo)
	 */
    @Override
    public LayTableResponse<ProjMaterialInfo> findPageByItems(MaterialInfoQueryInfo queries){
        PageHelper.startPage(queries.getPage(), queries.getLimit());
        List<ProjMaterialInfo> materialInfo = materialInfoViMapper.findPageByItems(queries);
        LayTableResponse<ProjMaterialInfo> po = new LayTableResponse<ProjMaterialInfo>(materialInfo);
        return po;
    }
    /**
     * 加载所有包含BOM信息的存货
     * @see com.qzdsoft.erpcloud.service.proj.MaterialInfoService#selectPageBy(com.qzdsoft.erpcloud.vo.proj.MaterialInfoQueryInfo)
     */
    @Override
    public LayTableResponse<ProjMaterialBomInfo> selectPageBy(
            MaterialInfoQueryInfo info){
        PageHelper.startPage(info.getPage(), info.getLimit());
        
        List<ProjMaterialBomInfo> materialInfo = materialInfoViMapper.findAllBomMaterial(info);
        LayTableResponse<ProjMaterialBomInfo> po = new LayTableResponse<ProjMaterialBomInfo>(materialInfo);
        return po;
    }
    /**
     * 根据存货的ID加载所有包含BOM版本的存货 
     * @see com.qzdsoft.erpcloud.service.proj.MaterialInfoService#selectBomVersionByMaterialId(com.qzdsoft.erpcloud.vo.proj.MaterialInfoQueryInfo)
     */
    @Override
    public LayTableResponse<ProjMaterialBomInfo> selectBomVersionByMaterialId(MaterialInfoQueryInfo info){
        PageHelper.startPage(info.getPage(), info.getLimit());
        List<ProjMaterialBomInfo> materialInfo = materialInfoViMapper.selectBomVersionByMaterialId(info);
        LayTableResponse<ProjMaterialBomInfo> po = new LayTableResponse<ProjMaterialBomInfo>(materialInfo);
        return po;
    }

}
